package com.umeox.watch.audiobooks.ui;


import android.content.Context;
import android.content.Intent;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;

import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.watch.audiobooks.R;


public class WelcomeActivity extends ThemeAppCompatActivity implements SoundPool.OnLoadCompleteListener {
    private static final String TAG = WelcomeActivity.class.getSimpleName();
    private SoundPool soundPool;
    private boolean isLoadCompleted;
    private int soundID;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initSoundPool();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        initSoundPool();
    }

    private void initSoundPool() {
        boolean playFlag = getIntent().getBooleanExtra("playFlag", false);
        if (!playFlag) {
            return;
        }

        if (soundPool == null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                soundPool = new SoundPool.Builder()
                        .setMaxStreams(1)
                        .setAudioAttributes(new AudioAttributes.Builder()
                                .setLegacyStreamType(AudioManager.STREAM_MUSIC)
                                .build())
                        .build();
            } else {
                soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
            }
            soundPool.setOnLoadCompleteListener(this);
            soundID = soundPool.load(this, R.raw.remind, 1);
            if (soundID == 0) {
                // 记载失败
                Log.w(TAG,"提醒铃声加载失败");
            } else {
                // 加载成功
                Log.i(TAG,"提醒铃声加载成功");
            }
        } else if (isLoadCompleted && soundID > 0) {
            float volume = getVolume();
            soundPool.play(soundID, volume, volume, 1, 0, 1);
        }
    }

    private float getVolume() {
        AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        return (float) currentVolume / maxVolume;
    }

    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        isLoadCompleted = true;
        float volume = getVolume();
        soundPool.play(sampleId, volume, volume, 1, 0, 1);
    }

    @Override
    protected void onPause() {
        super.onPause();
        release();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        release();
    }

    private void release(){
        if(soundPool != null){
            soundPool.release();
        }
    }


}
